package com.joshondesign.sdljava.demo;

import com.joshondesign.sdljava.SDLUtil;
import com.joshondesign.sdljava.SDL_Surface;
import com.joshondesign.sdljava.events.Gestures;
import com.joshondesign.sdljava.events.Keyboard;
import com.joshondesign.sdljava.gfx.Trigger;
import com.joshondesign.sdljava.gfx.*;

import static com.joshondesign.sdljava.events.Gestures.*;
import static com.joshondesign.sdljava.events.Gestures.MouseEvent;

public class EventsTest implements SDLUtil.InitCallback {
    public static void main(String ... args) {
        SDLUtil.init(new EventsTest());
    }

    public void callback() throws Exception {
        Util.standard_init();
        SDL_Surface surface = Util.createResizableWindow(800, 600);
        final Scene scene = new Scene(surface);
        scene.setBackgroundFill(SColor.BLACK);
        Layer layer = new Layer(surface.getFormat());
        layer.add(new Rect(20,30,100,60, SColor.BLUE));
        scene.add(layer);

        //listen to a mouse click anywhere on screen
        listen(MouseClick(), new Callback<MouseEvent>() {
            public void call(MouseEvent e) {
                Util.p("clicked on the screen at: " + e.getPoint());
            }
        });

        //shift command J anywhere in the app
        listen(KeyPressed(Keyboard.Keycode.J, Keyboard.Modifier.Command, Keyboard.Modifier.Shift),new Callback<KeyEvent>(){
            public void call(KeyEvent e) {
                Util.p("typed command shift J");
            }
        });

        //the same with shorthand
        listen(KeyPressed("command shift J"), new Callback<KeyEvent>(){
            public void call(KeyEvent keyEvent) {
                Util.p("typed command shift J (alternate form)");
            }
        });

        //the key 'X' is typed
        listen(KeyTyped(Keyboard.Keycode.X),new Callback<KeyEvent>(){
            public void call(KeyEvent keyEvent) {
                Util.p("typed 'x'");
            }
        });

        listen(KeyTyped(),new Callback<KeyEvent>(){
            public void call(KeyEvent keyEvent) {
                Util.p("typed " + keyEvent.getKeyCode());
            }
        });


        Callback quitHandler = new Callback() {
            public void call(Object o) {
                scene.exit();
            }
        };
        listen(KeyPressed(Keyboard.Keycode.Q, Keyboard.Modifier.Command), quitHandler);
        ButtonNode button = new ButtonNode(scene).setText("an action button");
        button.setWidth(250).setHeight(100).setY(100);
        layer.add(button);

        //a button was clicked
        listen(Action(button),new Callback<ActionEvent>(){
            public void call(ActionEvent actionEvent) {
                Util.p("Button fired an action event");
            }
        });

        //alternate form of 'a button was clicked'
        button.onAction(new Callback<ActionEvent>() {
            public void call(ActionEvent actionEvent) {
                Util.p("Button fired an action event (using onAction())");
            }
        });

        TextboxNode box = new TextboxNode();
        box.setWidth(250).setHeight(100).setY(200);
        layer.add(box);


        /*
        //letters are typed into a textfield
        listen(textfield,CharTyped(ANY),new Callback<CharEvent>());

        //device is rotated
        listen(ANY,ScreenRotate(),new Callback<RotationEvent>());

        //raw compass events. No 'ANY' because it's global
        listen(Compass(),new Callback<CompassEvent>());  e.getDegrees(); e.getRaw();

        //check the current keyboard state in a loop
        KeyStates.isPressed(Keys.J);

        //a two finger swipe
        listen(TwoFingerSwipe(),new Callback<MultitouchEvent>() {
                public void call(MultitouchEvent event) {
                    if(event.isLeft())

        //value of a slider has changed
        listen(slider, ValueChanged(), new Callback<ValueChangedEvent>() {
                public void call(ValueChangedEvent event);

        //alternate form
        slider.onValueChanged(new Callback<ValueChangedEvent>()) {

        //alternate form without generics
        slider.onValueChanged(new Callback() { public void call(Object o) {} });

        //value of a slider is changing
        listen(slider, DynamicValueChanged(), new Callback<ValueChangedEvent>());

        //focus changed
        listen(FocusChanged(), callback);

        //an object was dragged on screen
        listen(node,NodeDragged(),new Callback<NodeDragEvent>() {
                public void call(NodeDragEvent e) {
                    node.x = e.startNodeX + e.dragX;
                }
        });
        
        //get raw multitouch events
        listen(RawMultitouch(), new Callback<TouchEvent>());
        */

        //start() never returns
        scene.start();
    }
}
